<?php


namespace app\api\library\login\wxLogin;


use app\api\help\Http;
use app\api\model\MallUser;
use app\api\model\MallWxapp;

class V
{
    public static function run($param)
    {
        $code      = $param[ 'code' ];
        $res       = MallWxapp::where('wxapp_id', 10001)->find()->toArray();
        $appId     = $res[ 'app_id' ];
        $appSecret = $res[ 'app_secret' ];
        $c         = Http::get("https://api.weixin.qq.com/sns/jscode2session?appid=" . $appId . "&secret=" . $appSecret . "&js_code=" . $code . "&grant_type=authorization_code");
        $resArr    = json_decode($c, true);
        if (empty($resArr)) {
            _outPut(204, [], '获取用户信息失败');
        }
        if (isset($resArr[ 'errcode' ]) && $resArr[ 'errcode' ] !== 0) {
            _outPut(204, [], $resArr[ 'errmsg' ]);
        }
        if (empty($resArr[ 'openid' ])) {
            _outPut(204, [], '获取用户信息失败1');
        }
        $user = MallUser::where('open_id', $resArr[ 'openid' ])->find();
        if (empty($user)) {
            $userId = MallUser::insertGetId(['open_id' => $resArr[ 'openid' ], 'create_time' => time(), 'update_time' => time()]);
            $user   = MallUser::where('id', $userId)->find();
        }
        _output(200, $user);
    }
}